<script>
    $(document).ready(function () {

        //添加
        $("#add-sub").click(function () {
            var platfm_id = $("#release-platfm").val();
            var branch_name = $("#branch-name").val();
            var branch_msg = $("#branch-msg").val();
            var test_url = $("#test-url").val();
            $.ajax({
                type: "POST",
                async: false,
                url: "/item/addbranch/",
                data: {
                    "platfm_id": platfm_id,
                    "branch_name": branch_name,
                    "branch_msg": branch_msg,
                    "test_url": test_url
                },
                dataType: "json",
                success: function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit']").click(function () {
            var id = $(this).attr("edit_id");
            $.ajax({
                type: "GET",
                async: false,
                url: "/item/editbranch/",
                data: {"id": id},
                dataType: "json",
                success: function (data) {
                    $("#edit-release-platfm").val(data.PlatfmID);
                    $("#edit-branch-name").val(data.BranchName);
                    $("#edit-branch-msg").val(data.BranchMsg);
                    $("#edit-test-url").val(data.TestUrl);
                    $("#edit-sub").attr("edit_id", data.ID);
                    $("#editModal").modal("show");
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

        //提交修改内容
        $("#edit-sub").click(function () {
            var id = $(this).attr("edit_id");
            var platfm_id = $("#edit-release-platfm").val();
            var branch_name = $("#edit-branch-name").val();
            var branch_msg = $("#edit-branch-msg").val();
            var test_url = $("#edit-test-url").val();
            $.ajax({
                type: "PUT",
                async: false,
                url: "/item/editbranch/",
                data: {
                    "id": id,
                    "platfm_id": platfm_id,
                    "branch_name": branch_name,
                    "branch_msg": branch_msg,
                    "test_url": test_url
                },
                dataType: "json",
                success: function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除
        $("td>a[name='del']").click(function () {
            var id = $(this).attr('del_id');
            var statu = confirm("是否确认删除!");
            if (statu == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/item/delbranch/",
                    data: {"id": id},
                    dataType: "json",
                    success: function (data) {
                        spop({
                            template: data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()", 2100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索
        $("#search-key").bind('change', function () {
            var key = $(this).val();
            if (key != "") {
                var url = "/item/searchbranch/"+key+"/";

            } else {
                var url = "/item/branch/";
            }
            $("#search-a").attr("href", url);
        });

        //内测服更新
        $("td>a[name='update']").click(function () {
            var id = $(this).attr('up_id');
            var cur_version = $(this).parent().siblings("td[name='vs']").children().text();
            var statu = confirm("当前版本: "+cur_version+", 是否更新内测服？");
            if (statu == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/item/testupdate/",
                    data: {"id": id},
                    dataType: "json",
                    success: function (data) {
                        spop({
                            template: data,
                            style: 'success',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()", 2100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

    })
</script>
